// import resource from '../../lib/resource';
// import tips from '../../lib/tips';

Page({
  data: {
    // 设置菊花初始状态
    userId: '', //用户id
    loading: true,
    addressesList: [],
    wxaddressList: [],
    try: 'cy',
    defaultId: 0,
    tipsData: {
      title: ''
    },
    status: '' //当前状态
  },

  onLoad(options) {
    // console.log(options)
    //获取用户id
    let userId = wx.getStorageSync('userId');
    this.setData({
      userId: userId
    });
    this.getAddressList();
    this.setData({
      status: options.status
    })
  },


  /* 获取地址信息 */
  getAddressList() {
    var url = wx.getStorageSync("config")
    let that = this;
    let userId = that.data.userId;
    // console.log(userId)
    wx.request({
      url: url.service.homeUrl + "/huiguniang/address/fetch?openId=" + userId,
      // url: url.service.homeUrl + "/huiguniang/address/fetch?openId=orXxp5IJRbrsKSYzLpdNEUxEQL-M & id=1",
      method: 'GET',
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        console.log(res)
        if (res.data.status.returnCode === 1) {
          that.setData({
            addressesList: res.data.result,
            loading: false
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  setDefaultStyle(list, id) {
    list.forEach((itm) => {
      if (itm) {
        itm.items.is_default = +itm.address_id === id;
        itm.items.iconType = itm.items.is_default ? 'success' : 'circle';
        itm.items.iconColor = itm.items.iconType === 'success' ? '#FF2D4B' : '';
      }
    });
  },

  /* 跳转编辑界面 */
  goEdit(event) {
    console.log(event)
    const id = event.target.dataset.addressId;
    console.log(id)
    wx.navigateTo({
      url: '../address-edit/address-edit?id=' + id
    });
  },

  /* 删除 */
  delete(event) {
    var that = this;
    // 获取索引
    const index = event.target.dataset.index;
    // 订单id
    const id = event.target.dataset.addressId;
    // 获取地址列表数据
    let addressList = this.data.addressesList;

    wx.showModal({
      title: '提示',
      content: '确认删除吗',
      success: function(res) {
        var url = wx.getStorageSync("config")
        wx.request({
          url: url.service.homeUrl + "/huiguniang/address/delete?id=" + id,
          method: 'DELETE',
          header: {
            'Content-Type': 'application/json'
          },
          success: function(res) {
            console.log(res)
            if (res.data.status.code === 1) {
              wx.showModal({
                title: '删除成功！',
                showCancel: false, //是否显示取消按钮
                success: function(res) {
                  if (res.confirm) { //这里是点击了确定以后
                    that.getAddressList();
                  }
                }
              })
            }
          },
          fail: function(res) {},
          complete: function(res) {},
        })
      },
      fail: function(res) {
        // console.log(res);
      }
    })
  },

  setDefault(event) {
    // console.log(event)
    const checkedId = +event.currentTarget.dataset.valueId || +event.detail.value;
    let setFlag = false;
    // resource.loadingToast();
    // resource.setDefaultAddress(checkedId).then((res) => {
    //   if (res.statusCode === 200) {
    //     setFlag = true;
    //     this.setDefaultStyle(this.data.addressesList, checkedId);
    //     this.setData({ addressesList: this.data.addressesList });
    //   } else {
    //     setFlag = false;
    //   }
    //   return setFlag;
    // }).then((flag) => {
    //   if (flag) {
    //     wx.showToast({
    //       title: '默认地址设置成功',
    //       icon: 'success'
    //     });
    //   } else {
    //     // wx.failToast();
    //   }
    // });
  },

  /* 选择地址 */
  chooseAddress(e) {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    // 订单id
    const id = e.currentTarget.dataset.addressId;
    console.log("id" + id)
    let status = this.data.status;
    if (status === "choose") {
      wx.navigateBack({//返回
        delta: 1
      })
      prevPage.setData({
        addressId: id,
        status: "choose"
      })
    }
  },

  onShow() {
    this.getAddressList();
  },
  // addWxAddress() {
  //   let userId = this.data.userId;
  //   let addressList = {};
  //   if (wx.chooseAddress) {
  //     wx.chooseAddress({
  //       success: (res) => {
  //         console.log(res)
  //         addressList.openId = userId
  //         addressList.receiver = res.userName
  //         addressList.phone = res.telNumber
  //         // addressList.street =
  //         addressList.detail = res.detailInfo
  //         addressList.city = res.cityName
  //         addressList.county = res.countyName
  //         addressList.province = res.provinceName
  //         console.log(addressList)
  //         console.log(res)
  //         this.setData({
  //           wxaddressList: addressList
  //         })
  //         wx.redirectTo({
  //           url: "/pages/address-edit/address-edit?wx=" + 1 + "&receiver=" + res.userName + "&phone=" + addressList.phone + "&province=" + addressList.province + "&city=" + addressList.city + "&county=" + addressList.county + "&street=" + addressList.street + "&detail=" + addressList.detail,
  //         })
  //       }
  //     })
  //   } else {
  //     console.log('当前微信版本不支持chooseAddress');
  //   }
  // },
  
});


// wx.request({
//   url: url.service.homeUrl + "/huiguniang/address/add",
//   method: "POST",
//   header: { "Content-Type": "application/x-www-form-urlencoded" },
//   data: {
//     receiver: addressList.receiver,
//     phone: addressList.phone,
//     street: addressList.detail,
//     detail: addressList.detail,
//     province: addressList.province,
//     city: addressList.city,
//     country: addressList.county,
//     openId: userId
//   },
//   success: (res) => {
//     console.log("add成功")
//     console.log(res)
//   }
//   // success: function (res) {
//   //   console.log("add成功")
//   //   console.log(res)
//   //   if (res.data.status.returnCode === 1) {
//   //     wx.showModal({
//   //       title: '保存成功！',
//   //       showCancel: false, //是否显示取消按钮
//   //       success: function (res) {
//   //         if (res.confirm) { //这里是点击了确定以后
//   //           wx.navigateBack({
//   //             // delta: 1
//   //             url: "/pages/addresses/addresses",
//   //           })
//   //         }
//   //       }
//   //     })
//   //   }
//   // },
//   // fail: function (res) {
//   //   wx.showModal({
//   //     title: '保存失败，请重试！',
//   //     showCancel: false, //是否显示取消按钮
//   //     success: function (res) {
//   //       if (res.confirm) { //这里是点击了确定以后
//   //       }
//   //     }
//   //   })
//   // },
//   // complete: function (res) { },
// })

// wx.chooseAddress({
//   success: function (res) {
//     console.log(JSON.stringify(res))
//     console.log(res.cityName)
//     wx.request({
//       url: url.service.homeUrl + "/huiguniang/address/update",
//       method: "POST",
//       header: { 'Content-Type': 'application/json' },
//       data: {
//         country: res.cityName,
//         street: res.countyName,
//         detail: res.detailInfo,
//         id: id,
//         openId: userId,
//         receiver: res.userName,
//         phone: res.telNumber,
//         province: res.provinceName,
//         city: res.cityName
//       },
//       success: function (res) {
//         console.log(res)
//       }
//     })
//     var that = this
//     var obj = {}
//     obj.name = err.userName
//     obj.province = err.provinceName
//     obj.city = err.cityName
//     let addresses = that.data.addressesList
//     lists.push(obj)
//     that.setData({ addressesList });
//     console.log(that.data.addressesList)
//   },
//   fail: function (err) {
//   }
// })